




drawCharts=function(data) {



    if (type == "trend" || type == "analysis" ) {

        //console.log(data);
        try{
            $(".zy-charts-long .zy-table").empty();

            drawHeapBarToDiv("#wbjs",data.oneBar.wbjs,data.twoBar.wbjs,function(d){window.drawId="wbjs";dataFormat.queryData(d.data.BEGINMILEAGE,d.data.ENDMILEAGE);});
            drawHeapBarToDiv("#nbjs",data.oneBar.nbjs,data.twoBar.nbjs,function(d){window.drawId="nbjs";dataFormat.queryData(d.data.BEGINMILEAGE,d.data.ENDMILEAGE);});
            drawHeapBarToDiv("#ax",data.oneBar.ax,data.twoBar.ax,function(d){window.drawId="ax";dataFormat.queryData(d.data.BEGINMILEAGE,d.data.ENDMILEAGE);});
            drawHeapBarToDiv("#hf",data.oneBar.hf,data.twoBar.hf,function(d){window.drawId="hf";dataFormat.queryData(d.data.BEGINMILEAGE,d.data.ENDMILEAGE);});
            drawHeapBarToDiv("#lxhf",data.oneBar.lxhf,data.twoBar.lxhf,function(d){window.drawId="lxhf";dataFormat.queryData(d.data.BEGINMILEAGE,d.data.ENDMILEAGE);});
        }catch(e)
        {
            console.log(e);
        }

    } else {
        drawPieToDiv(data.noInspData, "noinspPie", "TYPE", "NUM",{"地质灾害":"#ff8500","第三方损伤":"#ffc300","事件上报":"#bd9bd7","直接评价":"#00a7d9","其它":"#4cc9ce"});
        drawPieToDiv(data.inspData, "inspPie", "TYPE", "NUM");
        try
        {

            drawBarToDiv(data.noInspBarData, "noInspBar", "MOTH", "NUM",{"地质灾害":"#ff8500","第三方损伤":"#ffc300","事件上报":"#bd9bd7","直接评价":"#00a7d9","其它":"#4cc9ce"});
        }catch(e)
        {
            console.log(e);
        }
    }

};
drawChartsOnssse=function(data)
{


    if(window[window.drawId+"_t"] == undefined)
    {
        window[window.drawId+"_t"] = 0;
    }
    window[window.drawId+"_t"]++;
    if( window[window.drawId+"_t"] > 3)
    {
        return ;
    }

    drawHeapBarToDiv("#"+window.drawId,data.result.oneBar[window.drawId],data.result.twoBar[window.drawId],function(d){dataFormat.queryData(d.data.BEGINMILEAGE,d.data.ENDMILEAGE);},true);

};


drawHeapBarToDiv =function (id,data1,data2,callFn,czId)
{
    if(czId == undefined)
    {
        var zid = id.substring(1);
        window[zid+"_t"] = 0;
    }


    $(id).data("d1",data1);
    $(id).data("d2",data2);
    $(id).data("d3",callFn);
    if($(id).find("img").length >0)
    {
        $(id).attr("noDataImg",$(id).find("img").attr("src"));
        $(id).attr("noDataImgStyle",$(id).find("img").attr("style"));
    }


    $(id).empty();
    var name=[];
    var d1=[];
    var d2=[];

    //排序函数
    var sort=[];
    var totale=0;
    for(var i=0;i< data1.length;i++)
    {
        //data2[i].NUM=20;
        //data1[i].NUM=10;

        name.push( parseFloat(data1[i].ENDMILEAGE).toFixed(1) );
        d1.push({value:data1[i].NUM,data:data1[i]});
        d2.push({value:data2[i].NUM,data:data2[i]});
        totale+=data2[i].NUM;
        totale+=data1[i].NUM;
        sort.push({"num":(data2[i].NUM-data1[i].NUM),"data":data1[i]});
    }


    sort.push({"num":(data2[0].NUM-data1[0].NUM),"data":data1[9]});
    name[name.length-1]=name[name.length-1]+"(m)";
    if(totale >0 ) {
        $(id).attr("data-status","true");
        drawClickBar(data1,data2,id,callFn);
        if( $(id+"text").length >0  )
        {
            var sortData=getSortMaxThree(sort);
            if(sortData[0].num >0)
            {
                $(id+"text").html("在绝对距离"+getShowData(sortData,0)+"范围内，"+getDivName(id)+"缺陷数量变化异常，需重点关注");
            }
            sortData=getSortMinThree(sort);
            if(sortData[0].num <0)
            {
                $(id+"text").html("在绝对距离"+getShowData(sortData,0)+"范围内，"+getDivName(id)+"缺陷数量变化异常，需重点关注");
            }
        }
        $(".zy-defect-text."+id.substring(1)).html("(m)");
        $(".zy-defect-text ").eq($(".zy-charts-tabs li.active").index()).show();
    }else
    {
        $(".zy-defect-text."+id.substring(1)).html("");
        $(id).attr("data-status","false");
        $(id+"text").html("");
        $(id).data("sort",null);
        $(id).html("<img src='"+$(id).attr("noDataImg")+"' style='"+$(id).attr("noDataImgStyle")+"'  />");
    }




};

drawClickBar=function(data1,data2,id,callFn)
{

    var mileageList = [];
    var ts = [];
    var t = null;
    var ts = null;
    for(var i = 0,e = data1.length;i < e;i++)
    {
        //if(data1[i].ENDMILEAGE > 1000){
        //    var v = parseFloat(data1[i].ENDMILEAGE).toFixed(0);
        //    v=v.substring(0, v.length -2);
        //    //判断上一个x坐标与当前坐标是否相同
        //    if(t != v)
        //    {
        //        t = v;
        //        mileageList.push(parseFloat(v+"00"));
        //    }else{
        //        mileageList.push("");
        //    }
        //
        //
        //}else{
        //    mileageList.push(parseFloat(data1[i].ENDMILEAGE).toFixed(0));
        //}
        var v = Math.round(parseFloat(data1[i].ENDMILEAGE))+"";
        if( i+1 <e )
        {
            if(parseFloat(data1[i+1].ENDMILEAGE)  - parseFloat(data1[i].ENDMILEAGE)  <10)
            {

               // v=v.substring(0, v.length) ;
            }else if(parseFloat(data1[i+1].ENDMILEAGE)  - parseFloat(data1[i].ENDMILEAGE)  <100)
            {

                v=v.substring(0, v.length -1) +"0";
            }else if(parseFloat(data1[i+1].ENDMILEAGE)  - parseFloat(data1[i].ENDMILEAGE)  >=100)
            {
                v=v.substring(0, v.length -2) +"00";
            }
            t = data1[i].ENDMILEAGE;
        }else{
            if(parseFloat(data1[i].ENDMILEAGE)  - parseFloat(t)  <10)
            {

                // v=v.substring(0, v.length) ;
            }else if(parseFloat(data1[i].ENDMILEAGE)  - parseFloat(t)  <100)
            {
                v=v.substring(0, v.length -1) +"0";

            }else if(parseFloat(data1[i].ENDMILEAGE)  - parseFloat(t)  >=100)
            {
                v=v.substring(0, v.length -2) +"00";

            }
        }

        if(ts != v)
            {
                t = v;
                mileageList.push(parseFloat(v));
            }else{
                mileageList.push("");
            }
        ts =v;
    }
    //mileageList[mileageList.length-1]=mileageList[mileageList.length-1]+"(m)";


    var data = [];
    for(var j = 0; j< 2; j++)
    {
        var t = [];
        for(var i = 0,e = data1.length;i < e;i++)
        {
            if(j == 0)
            {
                t.push(data1[i].NUM)
            }else{
                t.push(data2[i].NUM)
            }

        }
        data.push(t);
    }

    $(id).data("data",data1);
    $(id).data("callFn",callFn);
    var picECharts = new PicEChart();
    console.log(mileageList);
    picECharts.addMorebar({name1:'对比统计图',num:2,legends:["第一次","第二次"],color:["#37a4dd","#fe8e07"],barCategoryGap:"30%",barGap:"10%",Axisdata:mileageList,data:data,divObject:id.substring(1),legend:mileageList,onClickCall:function() {

        if (window[$(arguments[1].dom).attr("id") + "_t"] > 3) {
            return;
        }
        var d = {data: $(arguments[1].dom).data("data")[arguments[0].dataIndex]};
        $(arguments[1].dom).data("callFn")(d);
    }});
};




//根据ID 获取模块名称
getDivName=function(id)
{
    if(id == "#wbjs")
    {
        return "外部金属损失";
    }else if(id == "#nbjs")
    {
        return "内部金属损失";
    }else if(id == "#ax")
    {
        return "凹陷";
    }else if(id == "#hf")
    {
        return "环焊缝异常";
    }else if(id == "#lxhf")
    {
        return "螺旋焊缝异常";
    }
    return "";
};


getShowData =function(data,i)
{


    return parseFloat(parseFloat(data[i].data.BEGINMILEAGE).toFixed(3))+"-"+  parseFloat(parseFloat(data[i].data.ENDMILEAGE).toFixed(3));
};



getSortMinThree=function(data)
{
    var one={num:0};
    var two={};
    var three={};

    for(var i in data)
    {
        if(one.num > data[i].num)
        {
            three=two;
            two=one;
            one=data[i];

        }else if(two.num > data[i].num)
        {
            three=two;
            two=data[i];
        }else if(three.num > data[i].num)
        {
            three=data[i].num;
        }
    }

    var result=[one,two,three];

    return result;
};

getSortMaxThree=function(data)
{
    var one={num:0};
    var two={};
    var three={};

    for(var i in data)
    {
        if(one.num <data[i].num)
        {
            three=two;
            two=one;
            one=data[i];

        }else if(two.num <data[i].num)
        {
            three=two;
            two=data[i];
        }else if(three.num <data[i].num)
        {
            three=data[i].num;
        }
    }

    var result=[one,two,three];

    return result;
};


//dataFormat.queryData
drawChartsOne=function(data){

};


drawPieToDiv=function(data,id,valueName,value,colors)
{
    if($("#"+id).find("img").length >0)
    {
        $("#"+id).attr("noDataImg",$("#"+id).find("img").attr("src"));
        $("#"+id).attr("noDataImgStyle",$("#"+id).find("img").attr("style"));
    }


    var showdata=[];
    var totale=0;
    for(var i in data)
    {
        if(colors != undefined)
        {
            var qxName = data[i][valueName];
            //showdata.push({value:data[i][value],name:data[i][valueName],color:colors[data[i][valueName]]});
            if(data[i][valueName] == "地质灾害")
            {
                showdata[0]={value:data[i][value],name:data[i][valueName],color:colors[data[i][valueName]]};
            }else if(data[i][valueName] == "第三方损伤")
            {
                showdata[1]={value:data[i][value],name:data[i][valueName],color:colors[data[i][valueName]]};
            }else if(data[i][valueName] == "事件上报")
            {
                showdata[2]={value:data[i][value],name:data[i][valueName],color:colors[data[i][valueName]]};
            }else if(data[i][valueName] == "直接评价")
            {
                showdata[3]={value:data[i][value],name:data[i][valueName],color:colors[data[i][valueName]]};
            }else if(data[i][valueName] == "其它")
            {
                console.log(colors[qxName]);
                showdata[4]={value:data[i][value],name:data[i][valueName],color:colors[data[i][valueName]]};
            }


        }else{
            showdata.push({value:data[i][value],name:data[i][valueName]});
        }

        totale+=data[i][value];
    }
    if(totale >0 ) {
        var data = new Array();
        for(var i= 0,e=showdata.length;i<e;i++)
        {
            if(showdata[i] != undefined)
            {
                data.push(showdata[i]);
            }
        }
        console.log(data);
        var picECharts = new PicEChart();
        picECharts.drwaHollowPie({data: data, divObject: id,colors:["#fe8e07","#ffc501","#4cc9ce","#61d2fe","#37a4dd","#b39ddb","#8ce82a"],centerx:'30%'});
    }else{
        $("#"+id).html("<img src='"+$("#"+id).attr("noDataImg")+"' style='"+$("#"+id).attr("noDataImgStyle")+"'  />");


    }
};

drawBarToDiv =function (data,id,valueName,value,colors)
{

    if($("#"+id).find("img").length >0)
    {
        $("#"+id).attr("noDataImg",$("#"+id).find("img").attr("src"));
        $("#"+id).attr("noDataImgStyle",$("#"+id).find("img").attr("style"));
    }
    var totale=0;

    //获取月份
    var name={};

    //分类
    var type={};
    var typeList=[];
    for(var i=0;i<data.length;i++) {
        name[data[i][valueName]]="true";
        if(type[data[i]["TYPE"]] == null)
        {
            type[data[i]["TYPE"]]=[];
            typeList.push(data[i]["TYPE"]);
        }
    }



    //设置显示月份
    var Axisdata=[];
    for(var k in name)
    {
        Axisdata.push(k);
    }
    //设置分类默认值
    for(var k in type)
    {
        type[k]=getArrayDefualVal(Axisdata.length);
    }
    //按照分类 获取数据
    var showdata=[];
    for(var i=0;i<data.length;i++){



        //Axisdata.push(data[i][valueName]);
        var num =data[i][value];
        num=(num==null?"0":num);
        num=parseInt(num);
        //showdata.push(num);
        totale+=num;
        //console.log(data[i]["TYPE"] +"===>"+type);
        type[data[i]["TYPE"]]=setValToArray(type[data[i]["TYPE"]],num,Axisdata,[data[i][valueName]]);
    }

    if(totale >0)
    {
        if(Axisdata.length >1)
        {
            $("#"+id).css("width","100%");
        }else{
            $("#"+id).css("width","50%");
        }

        $("#"+id+"Tap").show();
        var picECharts = new PicEChart();
        var drawData = {"地质灾害":[],"第三方损伤":[],"事件上报":[],"直接评价":[],"其它":[]};
        for(var i in type)
        {
            drawData[i]=type[i];
        }
        //console.log(type);
        //console.log(drawData);
        picECharts.addMorebar({name1:'降水量',barCategoryGap:"30%",barGap:"10%",num:5,Axisdata:Axisdata,data:drawData,divObject:id,legend:typeList});
    }else{

        $("#"+id+"Tap").hide();
        $("#"+id).css("width","100%");
        $("#"+id).html("<img src='"+$("#"+id).attr("noDataImg")+"' style='"+$("#"+id).attr("noDataImgStyle")+"'  />");
    }




};


function setValToArray(array,val,showData,month)
{
    for(var i=0;i< showData.length ;i++)
    {
        if(showData[i] == month)
        {
            array[i]=val;
        }
    }
    return array;
}


//获取一个数组默认值
function getArrayDefualVal(num)
{
    var val=[];
    for(var i=0;i<num;i++)
    {
        val.push(0);
    }
    return val;
}


$(function(){

    $(".exportPdf").click(function(){
        var lineName=$('#JQ_pipeline').data('text');
        var name = lineName+"缺陷统计图";


        if($(this).attr("data-status") == "true")
        {
            var exportFile = $(this).EXPT({
                name:name ,
                yesBtnAct: function(callback){
                    //exportPDF(exportFile.name);
                    downPDF([$(".zy-table:eq(0)").get(0),$(".zy-table:eq(1)").get(0),$(".zy-table:eq(2)").get(0)],$('#JQ_pipeline').data('text')+"缺陷统计图",undefined,exportFile.name,{lineName:$('#JQ_pipeline').data('text'),beginName:$('#JQ_begin').data('text'),endName:$('#JQ_end').data('text')});
                    callback();
                }
            });
        }else{
            // edit by zy 2016/8/5
            // 增加为空提示
            $(this).MSG({
                type: 'error',
                content: '没有可导出的结果，请先查询！'
            });
        }

    });
    $(".exportTrendPdf").click(function(){
        var lineName=$('#JQ_pipeline').data('text');
        var name = lineName+(type=="trend"?"缺陷变化趋势": "缺陷数据对比分析图");
        if($(this).attr("data-status") == "true")
        {
            var exportFile = $(this).EXPT({
                name:name ,
                yesBtnAct: function(callback){
                    //exportPDF(exportFile.name);
                    //alert(exportFile.name);
                    downPDF([$(".zy-table:eq(0)").get(0) , $("#wbjs").get(0),$("#nbjs").get(0), $("#ax").get(0), $("#hf").get(0), $("#lxhf").get(0)],type=="trend"?$('#JQ_pipeline').data('text')+"缺陷变化趋势": $('#JQ_pipeline').data('text')+"缺陷数据对比分析图",resetData,exportFile.name);

                    callback();
                }
            });
        }else{
            // edit by zy 2016/8/5
            // 增加为空提示
            $(this).MSG({
                type: 'error',
                content: '没有可导出的结果，请先查询！'
            });
        }
    });
    $(".exportfocusPdf").click(function(){
        var lineName=$('#JQ_pipeline').data('text');
        var name = lineName+"重点关注信息";
        if($(this).attr("data-status") == "true")
        {
            var exportFile = $(this).EXPT({
                name:name ,
                yesBtnAct: function(callback){
                    //exportPDF(exportFile.name);
                    downPDF([$(".zy-table:eq(0)").get(0),$(".zy-table:eq(1)").get(0),$(".zy-table:eq(2)").get(0)],$('#JQ_pipeline').data('text')+"重点关注信息",undefined,exportFile.name,{lineName:$('#JQ_pipeline').data('text'),beginName:$('#JQ_begin').data('text'),endName:$('#JQ_end').data('text')});

                    callback();
                }
            });

        }else{
            // edit by zy 2016/8/5
            // 增加为空提示
            $(this).MSG({
                type: 'error',
                content: '没有可导出的结果，请先查询！'
            });
        }
    })

});


function resetData()
{
    drawHeapBarToDiv("#wbjs",$("#wbjs").data("d1"),$("#wbjs").data("d2"),$("#wbjs").data("d3"));
    drawHeapBarToDiv("#nbjs",$("#nbjs").data("d1"),$("#nbjs").data("d2"),$("#nbjs").data("d3"));
    drawHeapBarToDiv("#ax",$("#ax").data("d1"),$("#ax").data("d2"),$("#ax").data("d3"));
    drawHeapBarToDiv("#hf",$("#hf").data("d1"),$("#hf").data("d2"),$("#hf").data("d3"));
    drawHeapBarToDiv("#lxhf",$("#lxhf").data("d1"),$("#lxhf").data("d2"),$("#lxhf").data("d3"));
    var index=window.activeDiv;
    $(".zy-charts-tabs li").eq(index).addClass("active");
    $(".zy-charts-tabs li").eq(index).siblings().removeClass("active");

    $(".zy-defect-text ").hide();
    $(".zy-defect-text ").eq($(".zy-charts-tabs li.active").index()).show();

    $(".zy-charts-bodys div.zy-charts-body").css("display","none");
    $(".zy-charts-bodys div.zy-charts-body").eq(index).css("display","block");
    if($("#JQ_text_info").length >0)
    {
        $("#JQ_text_info div").css("display","none");
        $("#JQ_text_info div").eq(index).css("display","block");

    }
    window.activeDiv = undefined ;
}



function loadImgBase64(context,index)
{
    //console.log(context+"==========>"+$(context).is(":hidden"));




    if(type != undefined && !$(context).hasClass("zy-table"))
    {
        if(window.activeDiv == undefined)
        {
            window.activeDiv =  $(".zy-charts-tabs li.active").index();
        }

        $(".zy-charts-tabs li").eq($(context).index()).addClass("active");
        $(".zy-charts-tabs li").eq($(context).index()).siblings().removeClass("active");
        $(".zy-charts-bodys div.zy-charts-body").css("display","none");
        $(".zy-charts-bodys div.zy-charts-body").eq($(context).index()).css("display","block");
        $(".zy-defect-text ").hide();
        $(".zy-defect-text ").eq($(".zy-charts-tabs li.active").index()).show();
        if($("#JQ_text_info").length >0)
        {
            $("#JQ_text_info div").css("display","none");
            $("#JQ_text_info div").eq($(context).index()).css("display","block");
        }

        context=document.getElementById("test"+index);
        window.tIndex=index;
        setTimeout(outTimeExecute,1000);
        return ;


    }
    if($(context).hasClass("zy-charts-body"))
    {
        toCanvas($(".zy-table:eq(1)").get(0),index);
    }else{
        toCanvas(context,index);
    }


}

//1m执行
outTimeExecute=function()
{
    toCanvas($(".zy-table:eq(1)").get(0),window.tIndex);


};